import http from "@/api/config/http";
import { IQuery } from "@/interface/IAxios";
import { useCloned } from "@vueuse/core";
import { fileTemplate } from "../utils";
import { Media } from "./types";

export function fetchMediaList(query: IQuery) {
  const q = useCloned(query).cloned.value;
  if (q.mType) {
    q.mType = q.mType.join(",");
  }
  return http.request({
    url: `/media/device/list`,
    method: "get",
    params: q
  });
}
export function createMedia(data: Media) {
  return http.request({
    url: `/media/device`,
    method: "post",
    data
  });
}

export function updateMedia(data: Media) {
  return http.request({
    url: `/media/device`,
    method: "put",
    data
  });
}

export function getMediaById(id: string) {
  return http.request({
    url: `/media/device/${id}`,
    method: "get"
  });
}

export function deleteMedia(ids: string[]) {
  return http.request({
    url: `/media/device/${ids.join(",")}`,
    method: "delete"
  });
}

export function unboundMedia(mediaId: string) {
  return http.request({
    url: `/media/device/unbound`,
    method: "post",
    data: { mediaId }
  });
}

interface GoodsList {
  label: string;
  value: string;
}

type BoundData = {
  templateId?: string;
  mediaId: string;
  goodsList?: any[];
};
export function boundMedia(data: BoundData) {
  return http.request({
    url: `/media/device/bound`,
    method: "put",
    data
  });
}
interface PreviewMediaConfig {
  templateId: string;
  goodsList: GoodsList[];
}
export function previewMedia(data: PreviewMediaConfig) {
  return http.request({
    url: `/media/device/preview`,
    method: "post",
    data
  });
}

export const downloadManifest = () => {
  return http.request({
    url: `/media/device/manifest?scope=0`,
    method: "post"
  });
};

export const lcdManifest = () => {
  return fileTemplate("/media/device/manifest?scope=0");
};

export const lcdRelations = () => {
  return fileTemplate("/media/device/relations?scope=0");
};

type Firmware = {
  version: string;
  modelId: string;
};

interface LabelGroup {
  label: string;
  mediaIds: string[];
}

export const labelGroup = (data: LabelGroup) => {
  return http.request({
    url: `media/device/label`,
    method: "post",
    data
  });
};

export const fetchLabelOptions = (loadAll: boolean) => {
  return http.request({
    url: `media/device/label/options`,
    method: "get",
    params: {
      loadAll
    }
  });
};

export const automaticGrouping = () => {
  return http.request({
    url: `media/device/groups`,
    method: "get"
  });
};

type DeviceConfig = {
  display: string;
  mediaIds: string[];
  conn?: string;
};
export const deviceConfig = (config: DeviceConfig) => {
  return http.request({
    url: `media/device/config`,
    method: "post",
    data: config
  });
};

export const lcdRecover = (data: string[]) =>
  http.request({
    url: `media/device/recover`,
    method: "post",
    data
  });
type ChangeTheme = {
  mediaIds?: string[];
  labels: string[];
  themeId: string;
};
export const changeTheme = (data: ChangeTheme) => {
  return http.request({
    url: `media/device/change/theme/${data.themeId}`,
    method: "post",
    data
  });
};
export const mediaRecover = (data: string[]) => {
  return http.request({
    url: `media/device/recover`,
    method: "post",
    data
  });
};

type MediaLink = {
  datetime: string;
  mediaIds: string[];
};
export const mediaLink = (data: MediaLink) => {
  return http.request({
    url: `media/device/link`,
    method: "post",
    data
  });
};

interface MediaShift {
  list: string[];
  to: string;
}
export const mediaShift = (data: MediaShift) =>
  http.request({
    url: `media/device/shift`,
    method: "post",
    data
  });
type VersionType = {
  tags: string[];
  firmware: Firmware;
};
export const upgradeLcdDevice = (data: VersionType) =>
  http.request({
    url: `firmware/firmware/rgb/upgrade`,
    method: "post",
    data
  });
export const shiftByStore = (from: string, to: string) =>
  http.request({
    url: `media/device/shift`,
    method: "get",
    params: {
      from,
      to
    }
  });
